

<%@page import="javax.xml.parsers.DocumentBuilderFactory"%>
<%@page import="java.io.StringReader"%>
<%@page import="org.xml.sax.InputSource"%>
<%@page import="javax.xml.parsers.DocumentBuilder"%>
<%@page import="org.w3c.dom.CharacterData"%>
<%@page import="org.xml.sax.InputSource"%>

<%@page import="org.w3c.dom.Element"%>

<%@page import="org.w3c.dom.Document"%>
<%@page import="org.w3c.dom.NodeList"%>
<%@page import="org.w3c.dom.Node"%>

<%
    String xml = request.getParameter("xml");
    String xmlRecords = xml;
    DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(xmlRecords));
    Document doc = db.parse(is);
    String data = "";
    data += updateheader(doc);
    data += updateSwimLine(doc);
    data += updateCells(doc);
    data += updateFooter(doc);
    out.print(data);
%>


<%!
    String updateheader(Document doc) {
        String returnData = "";

        returnData += "<mxGraphModel grid=\"1\" guides=\"1\" tooltips=\"1\" connect=\"1\" fold=\"1\" page=\"0\" pageScale=\"1\" pageWidth=\"826\" pageHeight=\"1169\"><root><mxCell id=\"0\"/><mxCell id=\"1\" parent=\"0\" />";

        return returnData;
    }

    String updateFooter(Document doc) {

        String returnData = "";

        returnData += "</root></mxGraphModel>";

        return returnData;

    }

    String updateCells(Document doc) {
        String returnData = "";

        NodeList nodes = doc.getElementsByTagName("mxCell");

        for (int i = 0; i < nodes.getLength(); i++) {

            Element cell = (Element) nodes.item(i);
            if (!cell.getAttribute("id").equalsIgnoreCase("1")) {

                NodeList geometry = cell.getElementsByTagName("mxGeometry");
                Element geometryData = (Element) geometry.item(0);

                if (cell.hasAttribute("edge")) {

                    returnData += "<mxCell id=\"" + cell.getAttribute("id") + "\"  value=\"" + cell.getAttribute("name") + "\" source=\"" + cell.getAttribute("source") + "\" target=\"" + cell.getAttribute("target") + "\" style=\"" + cell.getAttribute("style") + "\" edge=\"1\"     parent=\"" + cell.getAttribute("parent") + "\" >"
                            + "<mxGeometry relative=\"1\" as=\"geometry\"/></mxCell>";

                } else {

                    String ITEM = "";
                    if (cell.hasAttribute("itemid")) {
                        ITEM = " itemid=\"" + cell.getAttribute("itemid") + "\"";
                    }

                    returnData
                            += "<AllCell  " + ITEM + " label=\"" + cell.getAttribute("value") + "\"  id=\"" + cell.getAttribute("id") + "\">"
                            + "<mxCell   style=\"" + cell.getAttribute("style") + "\"  vertex=\"1\"    parent=\"" + cell.getAttribute("parent") + "\" >"
                            + "<mxGeometry x=\"" + geometryData.getAttribute("x") + "\" y=\"" + geometryData.getAttribute("y") + "\" width=\"" + geometryData.getAttribute("width") + "\" height=\"" + geometryData.getAttribute("height") + "\" as=\"geometry\"/>"
                            + "</mxCell>"
                            + "</AllCell>";

                }

            }

        }

        return returnData;
    }

    String updateSwimLine(Document doc) {
        String returnData = "";

        NodeList nodes = doc.getElementsByTagName("swimlane");
        for (int i = 0; i < nodes.getLength(); i++) {

            Element swimlane = (Element) nodes.item(i);

            NodeList geometry = swimlane.getElementsByTagName("geometry");
            Element geometryData = (Element) geometry.item(0);

            NodeList column = swimlane.getElementsByTagName("column");

            for (int c = 0; c < column.getLength(); c++) {
                Element columnData = (Element) column.item(c);

                String ITEM = "";
                if (columnData.hasAttribute("itemid")) {

                    ITEM = " itemid=\"" + columnData.getAttribute("itemid") + "\"  ";
                }

                String parent = swimlane.getAttribute("parent");
                if (columnData.hasAttribute("parentColumn")) {

                    parent = columnData.getAttribute("parentColumn");

                }

                returnData += "<AllCell  " + ITEM + "   label=\"" + columnData.getAttribute("name") + "\"  id=\"" + columnData.getAttribute("id") + "\">"
                        + " <mxCell    style=\"" + columnData.getAttribute("headerStyle") + "\"  vertex=\"1\"    parent=\"" + parent + "\" >"
                        + "<mxGeometry x=\"" + columnData.getAttribute("posX") + "\" y=\"" + columnData.getAttribute("posY") + "\" width=\"" + columnData.getAttribute("width") + "\" height=\"" + geometryData.getAttribute("height") + "\" as=\"geometry\"/>"
                        + "</mxCell>"
                        + "</AllCell>";

            }

        }

        return returnData;
    }

%>

